<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>

        <el-tabs v-model="activeName" @tab-click="handleClick">

          <el-tab-pane label="角色管理" name="first">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="clickShowAdd"
            >新增角色</el-button>
            <el-table
              v-loading="loading"
              :data="list"
              style="width: 100%"
            >
              <el-table-column

                label="序号"
                width="180"
                type="index"
                :index="indexMethod"
              />
              <el-table-column
                prop="name"
                label="角色名称"
                width="180"
              />
              <el-table-column
                prop="description"
                label="描述"
              />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="showEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>

            </el-table>
            <el-pagination
              :current-page="page"
              :page-sizes="[1,2,3,4,5]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second"> <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>

        <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
          <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入角色名称" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="form.description" placeholder="请输入角色描述" />
            </el-form-item>
          </el-form>

          <template #footer>
            <el-button @click="btnCancel">取消</el-button>
            <el-button type="primary" @click="submitBtn">确认</el-button>
          </template>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>

import { reqGetRoleList, reqDeleteRole, reqAddRole, reqUpdateRole, reqGetRoleDetail } from '@/api/setting'
import { mapState } from 'vuex'
import { reqGetCompanyById } from '@/api/company'

export default {
  name: 'Setting',

  data() {
    return {
      companyForm: '',
      showDialog: false,
      activeName: 'first',
      list: [],
      page: 1,
      pagesize: 2,
      total: 1,
      loading: false,

      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    ...mapState('user', ['userInfo']),
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    handleClick(tab, event) {
      // console.log(tab, event)
    },
    async getRoleList() {
      this.loading = true
      const res = await reqGetRoleList(this.page, this.pagesize)
      // console.log(res)
      const { total, rows } = res.data
      this.total = total
      this.list = rows
      this.loading = false
    },
    delRole(id) {
      this.$confirm('确认要删除该角色吗?', '温馨提示').then(async() => {
        await reqDeleteRole(id)
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.$message.success('恭喜!删除成功')
        this.getRoleList()
      }).catch(error => {
        console.log(error)
      })
    },
    // 修改了每页条数后, 会造成页数的变化, 推荐从第一页重新开始展示
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      // 更新每页条数
      this.pagesize = val
      // 重置当前页
      this.page = 1
      // 调用方法, 重新请求
      this.getRoleList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      // 更新当前页
      this.page = val
      // 调用方法, 重新请求渲染
      this.getRoleList()
    },
    indexMethod(index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    closeDialog() {
      this.showDialog = false
      this.form = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    clickShowAdd() {
      this.showDialog = true
    },
    btnCancel() {
      this.showDialog = false
    },
    submitBtn() {
      this.$refs.roleForm.validate(async flag => {
        if (!flag) return
        if (this.form.id) {
          // 编辑操作
          await reqUpdateRole(this.form)
        } else {
          // 添加操作
          await reqAddRole(this.form)
        }
        // 关闭弹框
        this.closeDialog()
        // 重新渲染
        this.getRoleList()
        // 添加成功
        this.$message.success('添加成功')
      })
    },

    async showEdit(id) {
      const { data } = await reqGetRoleDetail(id)
      this.form = data
      this.showDialog = true
    },
    async getCompanyInfo() {
      const res = await reqGetCompanyById(this.userInfo.companyId)
      this.companyForm = res.data
      // console.log(res)
    }
  }
}

</script>
